//
//  HJTabbar.m
//  newOcWeibo
//
//  Created by heJing on 15/7/20.
//  Copyright (c) 2015年 heima. All rights reserved.
//

#import "HJTabbar.h"
@interface HJTabbar()

// 加号按钮
@property(nonatomic, weak)UIButton *plusBtn;

@end

@implementation HJTabbar


-(void)layoutSubviews
{
    // 1. 先让系统把自己的事情做完
    [super layoutSubviews];

    // 2. 计算控件的位置
    CGFloat tabBarW = self.width / 5;
    CGFloat tabBarH = self.height;
    CGFloat tabBarY = 0;
    CGFloat tabBarX = 0;
    NSUInteger tabBarIndex = 0; // 索引
    
    for (UIView *subView in self.subviews) {
        
        // NSClassFromString(@"UITabBarButton") == [UIControl class]]
        if([subView isKindOfClass:[UIControl class]]){
        
            tabBarX = tabBarW * tabBarIndex;
        
            subView.frame = CGRectMake(tabBarX, tabBarY, tabBarW, tabBarH);
            
            tabBarIndex++;
            
            //tabBarIndex等于 2 的时候多挪个位置出来
            if (tabBarIndex == 2) tabBarIndex++;
            
        }
    }
    
    // 3. 设置中间按钮
    // 设置加号按钮的frame
    CGPoint tempCenter = self.plusBtn.center;
    tempCenter.x = self.width * 0.5;
    tempCenter.y = self.height * 0.5;
    self.plusBtn.center = tempCenter;

}

#pragma lazy
- (UIButton *)plusBtn
{
    if (!_plusBtn) {
        // 1.创建加号按钮
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [btn addTarget:self action:@selector(compose) forControlEvents:UIControlEventTouchUpInside];
        
        // 2.设置按钮的image图片
        [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        
        // 3.设置按钮的背景Image图片
        [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        
        // 4. 添加到父控件
        [self addSubview:btn];
        _plusBtn = btn;
    }
    return _plusBtn;
}


-(void)compose
{
    HJLog(@"%s",__func__);
}


@end
